

<title>管理员</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>用户</cite></a>
        <a><cite>普通用户</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">用户列表</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="test-table-page" lay-filter="test"></table>
                </div>
            </div>
        </div>
    </div>
</div>


<script type="text/html" id="toolbarlist">
    <div class="layui-form-item">
        <div class="layui-input-inline">
        </div>
    </div>
</script>

<script>
    layui.use(['jquery', 'table', 'admin'], function(){
        var $ = layui.$, form = layui.form,
            table = layui.table, admin = layui.admin, view = layui.view;
        var userlistUrl = "/uclass/admin/user";
        var specialtyMap = {};
        var specialtyUrl = "/uclass/elective/specialty";


        function getSpecialty(year, specialty_code) {
            if (specialtyMap.hasOwnProperty(specialty_code)) {
                return specialtyMap[specialty_code];
            }else  {
                $.ajax({
                    url: specialtyUrl + "?year=" + year,
                    dataType: 'json',
                    async: false,
                    complete: function (result) {
                        result = result.responseJSON;
                        specialtyList = result.data;
                        specialtyList.forEach(function (specialty) {
                            specialtyMap[specialty.code] = specialty.name;
                        });
                        return specialtyMap[specialty_code];
                    }
                });
            }
        }

        function getGender(type) {
            if (type === "1") {
                return "男";
            }else if (type === "2") {
                return "女";
            }
            return "未知";
        }

        var tablelns = table.render({
            elem: '#test-table-page'
            ,url: userlistUrl
            ,toolbar: '#toolbarlist'
            ,cols: [[
                {field:'usercode',  title: '学号', sort: true, align: 'center'}
                ,{field:'name',  title:'姓名',  align:'center'}
                ,{field:'specialty', title:'专业', align:'center', templet: function (d) {
                    var temp = getSpecialty(d.grade, d.specialty);
                    while (temp === undefined) {
                        temp = getSpecialty(d.grade, d.specialty);
                    }
                    return temp; }}
                ,{field:'gender', title:'性别', aligh:'center', templet: function (d) {return getGender(d.gender);}}
                ,{field:'grade', title:'年级', aligh:'center'}
            ]]
            ,page: true
            ,parseData: function(res) {
                return {
                    "status": res.status,
                    "message": res.message,
                    "total": res.data.total,
                    "list": res.data.userlist
                };
            }
            ,response: {
                statusName: 'status'
                ,statusCode: 200
                ,msgName: 'message'
                ,countName: 'total'
                ,dataName: 'list'
            }
        });

    });
</script>